//
//  LoginControl.m
//  FreeTime
//
//  Created by ti on 16/8/26.
//  Copyright © 2016年 SRDZ.com. All rights reserved.
//
//CGRectGetWidth(self.spaceBackV.frame) CGRectGetHeight(self.spaceBackV.frame)

#import "LoginControl.h"
#import "Header.h"
#import "LostPasswordControl.h"
#import "RegisterControl.h"
#import "AppDelegate.h"


#define SpaceWidth self.spaceView.frame.size.width
#define SpaceHeight self.spaceView.frame.size.height
@interface LoginControl ()
@property (nonatomic, strong)UIImageView *backRedBackV;
@property (nonatomic, strong)UIImageView *backLoginV;
@property (nonatomic, strong)UIImageView *spaceBackV;
@property (nonatomic, strong)UIImageView *imageV;
@property (nonatomic, strong)UIImageView *lockV;
@property (nonatomic, strong)UITextField *tf;

@property (nonatomic, strong)UIView *spaceView;
@property (nonatomic, strong)UIButton *loginBt;
@property (nonatomic, strong)UIButton *titleBt;
@end

@implementation LoginControl
- (void)viewWillAppear:(BOOL)animated {
//    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupNav];
    self.backRedBackV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_red"]];
    self.backRedBackV.frame = self.view.bounds;
    [self.view addSubview:self.backRedBackV];
    
    self.backLoginV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_login"]];
    self.backLoginV.frame = CGRectMake(WindowWidth/2-65, 78, 130, 19);
    [self.backRedBackV addSubview:self.backLoginV];
    
    self.spaceView = [[UIView alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.backLoginV.frame)+36, WindowWidth-40, 5.0/4*(WindowWidth-40))];
    [self.view addSubview:self.spaceView];
    
    self.spaceBackV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_space"]];
    self.spaceBackV.frame = self.spaceView.bounds;
    [self.spaceView addSubview:self.spaceBackV];
    
    NSArray *arr = @[@"请输入手机号",@"请输入密码"];
    NSArray *imageArr = @[@"login_phone",@"login_lock"];
    NSArray *titleArr = @[@"忘记密码",@"注册用户"];
    NSArray * titleColorArr = @[[ImageUtil colorWithRgb:0xff5757],[ImageUtil colorWithRgb:0x666666]];
    for (int i = 0; i<2; i++) {
        self.imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[imageArr objectAtIndex:i]]];
        self.imageV.frame = CGRectMake(17.5, 2.0/7*SpaceHeight+i*(1.2/7*SpaceHeight), 25, 25);
        [self.spaceView addSubview:self.imageV];
        
        self.tf = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.imageV.frame)+10, 2.0/7*SpaceHeight+i*(1.2/7*SpaceHeight), 200, 20)];
        self.tf.placeholder = [arr objectAtIndex:i];
        self.tf.font = FONT(14);
        self.tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.tf.autocorrectionType = UITextAutocorrectionTypeNo;
        self.tf.returnKeyType = UIReturnKeyDone;
        self.tf.tag = 100+i;
        self.tf.delegate = self;
        if (self.tf.tag == 100) {
            self.tf.keyboardType = UIKeyboardTypeNumberPad;
        }
        [self.tf addTarget:self action:@selector(LoginPassTF:) forControlEvents:UIControlEventEditingChanged];
        [self.spaceView addSubview:self.tf];
        
        [ToolUtil createLine:2.5/7*SpaceHeight+i*(1.2/7*SpaceHeight) superView:self.spaceBackV lift:17.5 width:SpaceWidth-35 hight:1.0 color:[ImageUtil colorWithRgb:0xe6e6e6]];
        
        CGFloat x = [ToolUtil autoWidth:@"忘记密码" width:0 height:12 num:12];
        self.titleBt = [UIButton buttonWithType:UIButtonTypeCustom];
        self.titleBt.frame = CGRectMake(15+i*(SpaceWidth-66-x), 6.2/7*SpaceHeight, x+40, 40);
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:self.titleBt.bounds];
        titleLabel.text = [titleArr objectAtIndex:i];
        titleLabel.textColor = [titleColorArr objectAtIndex:i];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = FONT(12);
        [self.titleBt addSubview:titleLabel];
//        _titleBt.backgroundColor = DEBUG_CLOLR;
        self.titleBt.tag = 20+i;
        [self.titleBt addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.spaceView addSubview:self.titleBt];
    }
    
    
    
    
    CGFloat w = _spaceView.frame.size.width;
    self.loginBt = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginBt.frame = CGRectMake(50, 4.7/7*SpaceHeight, w-100, 50);
    [_loginBt setBackgroundImage:[UIImage imageNamed:@"querenbiao"] forState:UIControlStateNormal];
    UILabel * LoginLbl = [ReduceCode creatLabelWithFrame:CGRectMake((w-180)/2, 1.5, 80, 40) title:@"完成" titleColor:[UIColor whiteColor] font:14 textAlignment:1];
    [_loginBt addSubview:LoginLbl];
    
    
    [self.loginBt addTarget:self action:@selector(LoginClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.spaceView addSubview:self.loginBt];
}
#pragma mark --UIButton Click----
- (void)LoginClick:(UIButton *)btn {
     NSLog(@"登录");
    AppDelegate * delegate = [UIApplication sharedApplication].delegate;
    delegate.window.rootViewController = [delegate creatTab];
}
- (void)titleClick:(UIButton *)btn {
    if (btn.tag == 20) {
        NSLog(@"忘记密码");
        LostPasswordControl * lostVC = [LostPasswordControl new];
        UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:lostVC];
//        [self.navigationController pushViewController:lostVC animated:YES];
        [self presentViewController:nav animated:YES completion:nil];
    }else if(btn.tag == 21){
        NSLog(@"注册用户");
        UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:[RegisterControl new]];
        //        [self.navigationController pushViewController:lostVC animated:YES];
        [self presentViewController:nav animated:YES completion:nil];
//        [self presentViewController:[RegisterControl new] animated:YES completion:nil];

//        [self.navigationController pushViewController:[RegisterControl new] animated:YES];

    }
}
#pragma mark  ---UITextField---
- (void)LoginPassTF:(UITextField *)textfield {
    
    if (textfield.tag == 100) {
        
        if (textfield.text.length>11) {
                textfield.text = [textfield.text substringToIndex:11];
            }
    }else if (textfield.tag == 101) {
        [textfield setSecureTextEntry:YES];
        
    }
   
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}




























@end
